<template>
  <div id="c2"></div>
</template>
<script>
import G2 from '@antv/g2'
export default {
  data () {
    return {
      data: [
        { item: '第一季度', count: 40, percent: 0.4 },
        { item: '第二季度', count: 21, percent: 0.21 },
        { item: '第三季度', count: 17, percent: 0.17 },
        { item: '第四季度', count: 13, percent: 0.13 },
        { item: '第五季度', count: 9, percent: 0.09 }
      ],
      chart: null
    }
  },
  mounted () {
    this.initComponent()
  },
  methods: {
    initComponent () {
      const chart = new G2.Chart({
        container: 'c2',
        forceFit: true,
        height: 300,
        padding: 'auto'
      })
      chart.source(this.data, {
        percent: {
          formatter: val => {
            val = (val * 100) + '%'
            return val
          }
        }
      })
      chart.coord('theta', {
        radius: 0.75,
        innerRadius: 0.6
      })
      chart.tooltip({
        showTitle: false,
        itemTpl: '<li><span style="background-color:{color};" class="g2-tooltip-marker"></span>{name}: {value}</li>'
      })
      chart.guide().html({
        position: ['50%', '50%'],
        html: '<div style="color:#8c8c8c;font-size: 14px;text-align: center;width: 10em;">交易额<br><span style="color:#8c8c8c;font-size:20px">200</span>万</div>',
        alignX: 'middle',
        alignY: 'middle'
      })
      const interval = chart.intervalStack()
        .position('percent')
        .color('item')
        .label('percent', {
          formatter: (val, item) => {
            return item.point.item + ': ' + val
          }
        })
        .tooltip('item*percent', (item, percent) => {
          percent = percent * 100 + '%'
          return {
            name: item,
            value: percent
          }
        })
        .style({
          lineWidth: 1,
          stroke: '#fff'
        })
      chart.render()
      interval.setSelected()
    }
  }
}
</script>
